﻿using Common.Contract;
using Common.Contract.CrossSelling.Request;
using Common.Contract.CrossSelling.Response;
using Common.Contract.ProductHandbook;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Contract.SystemConfig.Record;
using Common.Service;
using Common.Service.DataTables;
using Common.Type;
using SMECustomerWebsite.Core.Configuration;
using SMECustomerWebsite.Filters;
using SMECustomerWebsite.Models.ViewModels.ProductHandbook;
using SMECustomerWebsite.Models.ViewModels.SalePerformance;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;

namespace SMECustomerWebsite.Controllers
{
    public class ProductHandbookController : ControllerBase
    {
        #region Overview
        //
        // GET: /ProductHandbook/

        [CustomActionFilter]
        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult ByProduct()
        {
            ProductHandbookViewModel vm = new ProductHandbookViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveProductHandbookCategoryRequest request = new RetrieveProductHandbookCategoryRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.ByProduct"
                    },
                    Lang = CurrentLang,
                };
                var customerRecords = new RetrieveProductHandbookCategoryResponse();
                customerRecords = serviceClient.Proxy.RetrieveProductHandbookCategory(request);
                vm.productCat = (customerRecords == null || customerRecords.Records == null) ? new List<ProductHandbookCategoryRecord>() : customerRecords.Records;
            }
            return View(vm);
        }
        [CustomActionFilter]
        [Authorize]
        public ActionResult ByProductPro()
        {
            ProductHandbookViewModel vm = new ProductHandbookViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveProductHandbookCategoryRequest request = new RetrieveProductHandbookCategoryRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.ByProductPro"
                    },
                    Lang = CurrentLang,
                };
                var customerRecords = new RetrieveProductHandbookCategoryResponse();
                customerRecords = serviceClient.Proxy.RetrieveProductProgramHandbookCategory(request);
                vm.productCat = (customerRecords == null || customerRecords.Records == null) ? new List<ProductHandbookCategoryRecord>() : customerRecords.Records;
            }
            return View(vm);
        }

        #endregion
        #region Product Detail
        [CustomActionFilter]
        [Authorize]
        public ActionResult ProductDetail(string productId)
        {
            ProductHandbookProductDetailViewModel vm = new ProductHandbookProductDetailViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveProductHandbookProductDetailByProductIdRequest request = new RetrieveProductHandbookProductDetailByProductIdRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.ProductDetail"
                    },
                    Lang = CurrentLang,
                    ProductId = string.IsNullOrEmpty(productId) ? 0 : Convert.ToInt32(productId)
                };
                var customerRecords = new RetrieveProductHandbookProductDetailByProductIdResponse();
                customerRecords = serviceClient.Proxy.RetrieveProductHandbookProductDetailByProductId(request);
                vm.ProductDetail = (customerRecords == null || customerRecords.Record == null) ? new ProductHandbookProductDetailRecord() : customerRecords.Record;
            }
            return View(vm);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult ViewChecklist(string ccid)
        {
            ProductHandbookCustomerChecklistViewModel vm = new ProductHandbookCustomerChecklistViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistCustomerByCcidRequest request = new RetrieveChecklistCustomerByCcidRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.ProductDetail"
                    },
                    Lang = CurrentLang,
                    Ccid = string.IsNullOrEmpty(ccid) ? 0 : Convert.ToInt32(ccid),
                    CurrentUser = CurrentUserRecord
                };
                var customerRecords = new RetrieveChecklistCustomerByCcidResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistCustomerByCcid(request);
                vm.checklistCustomer = (customerRecords == null || customerRecords.Record == null) ? new ChecklistCustomerRecord() : customerRecords.Record;
            }
            return View(vm);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult EditChecklist(string ccid)
        {
            ProductHandbookCustomerChecklistViewModel vm = new ProductHandbookCustomerChecklistViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistCustomerByCcidRequest request = new RetrieveChecklistCustomerByCcidRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.ProductDetail"
                    },
                    Lang = CurrentLang,
                    Ccid = string.IsNullOrEmpty(ccid) ? 0 : Convert.ToInt32(ccid),
                    CurrentUser = CurrentUserRecord
                };
                var customerRecords = new RetrieveChecklistCustomerByCcidResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistCustomerByCcid(request);
                vm.checklistCustomer = (customerRecords == null || customerRecords.Record == null) ? new ChecklistCustomerRecord() : customerRecords.Record;
            }
            return View(vm);
        }
        [CustomActionFilter]
        [Authorize]
        public ActionResult EditProductDetail(string productId)
        {
            ProductHandbookProductDetailViewModel vm = new ProductHandbookProductDetailViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveProductHandbookProductDetailByProductIdRequest request = new RetrieveProductHandbookProductDetailByProductIdRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.EditProductDetail"
                    },
                    Lang = CurrentLang,
                    ProductId = string.IsNullOrEmpty(productId) ? 0 : Convert.ToInt32(productId)
                };
                var customerRecords = new RetrieveProductHandbookProductDetailByProductIdResponse();
                customerRecords = serviceClient.Proxy.RetrieveProductHandbookProductDetailByProductId(request);
                vm.ProductDetail = (customerRecords == null || customerRecords.Record == null) ? new ProductHandbookProductDetailRecord() : customerRecords.Record;
            }
            return View(vm);
        }
        [Authorize]
        [HttpPost]
        public ActionResult EditProductDetail(ProductHandbookProductDetailViewModel vm, FormCollection form)
        {
            try
            {
                using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
                {
                    ResponseRecord response = null;
                    UpdateProductHandbookProductDetailRequest request = new UpdateProductHandbookProductDetailRequest
                    {
                        Header = new RequestHeaderRecord
                        {
                            ApplicationKey = SystemConfiguration.ApplicationKey,
                            CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.EditProductDetail"
                        },
                        Record = new ProductHandbookProductDetailRecord
                        {
                            Overview = vm.ProductDetail.Overview,
                            Condition = vm.ProductDetail.Condition,
                            Document = vm.ProductDetail.Document,
                            Xselling = vm.ProductDetail.Xselling,
                        },
                        Lang = CurrentLang
                    };
                    response = serviceClient.Proxy.UpdateProductHandbookProductDetail(request);
                    if (response.IsSuccess)
                    {
                        return RedirectToAction("AccountPlanning", "PortfolioManagement");
                    }
                    else
                    {
                        vm.Message = response.ErrorMessage;
                        vm.MessageType = MessageType.Error;
                        //vm.IsSupperAdmin = CurrentUser.Equals(SystemConfiguration.SUPPER_ADMIN, StringComparison.OrdinalIgnoreCase);
                    }
                    //record = customerRecords.Records.FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                vm.MessageType = MessageType.Error;
                vm.Message = ex.ToString();
            }
            return View(vm);
        }
        #endregion

        #region Product Checklist
        [CustomActionFilter]
        [Authorize]
        public ActionResult Checklist(string productId)
        {
            ProductHandbookChecklistViewModel vm = new ProductHandbookChecklistViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistQuestionByProductRequest request = new RetrieveChecklistQuestionByProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.EditProductDetail"
                    },
                    Lang = CurrentLang,
                    ProductId = productId,
                    CurrentUser = CurrentUserRecord
                };
                var customerRecords = new RetrieveChecklistQuestionByProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistQuestionByProduct(request);
                vm.listQuestion = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistQuestionRecord>() : customerRecords.Records;
            }
            vm.CurrentUser = CurrentUserRecord;
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult ListCustomerChecklistData(DataTablesPageRequest pageRequest, string productId)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistCustomerByDaoRequest request = new RetrieveChecklistCustomerByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.Checklist"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    ProductId = productId
                };
                var customerRecords = new RetrieveChecklistCustomerByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistCustomerByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistCustomerRecord>() : customerRecords.Records;
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "4":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Cif,
                                                             x.CustName,
                                                             x.LegalId,
                                                             x.DateAdd.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.DateUpdate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Ccid,
                        }).ToList()
                        };
                        break;
                    case "3":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CustName,
                                                             x.LegalId,
                                                             x.DateAdd.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.DateUpdate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Ccid,
                        }).ToList()
                        };
                        break;
                    case "2":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Branch,
                                                             x.Dao,
                                                             x.DaoName,
                                                             x.Cif,
                                                             x.CustName,
                                                             x.LegalId,
                                                             x.DateAdd.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.DateUpdate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Ccid,
                        }).ToList()
                        };
                        break;
                    case "1":
                        pageResponse = new DataTableResponse
                        {
                            sEcho = pageRequest.Echo,
                            iTotalRecords = customerRecords.Page.TotalRecords,
                            sColumns = pageRequest.ColumnNames,
                            aaData = resultCustomer.Select(x => new object[]{
                                                             x.Region,
                                                             x.Branch,
                                                             string.IsNullOrEmpty(x.Dao) ? string.Empty:x.Dao,
                                                             string.IsNullOrEmpty(x.DaoName)? string.Empty: x.DaoName,
                                                             x.Cif,
                                                             x.CustName,
                                                             x.LegalId,
                                                             x.DateAdd.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.DateUpdate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Ccid,
                        }).ToList()
                        };
                        break;
                    default:
                        break;
                }
            }
            return Json(pageResponse);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult CustChecklist(string productId)
        {
            ProductHandbookChecklistViewModel vm = new ProductHandbookChecklistViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistQuestionByProductRequest request = new RetrieveChecklistQuestionByProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.EditProductDetail"
                    },
                    Lang = CurrentLang,
                    ProductId = productId,
                    CurrentUser = CurrentUserRecord
                };
                var customerRecords = new RetrieveChecklistQuestionByProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistQuestionByProduct(request);
                vm.listQuestion = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistQuestionRecord>() : customerRecords.Records;
            }
            return View(vm);
        }
        [Authorize]
        [HttpPost]
        public ActionResult ChecklistDocumentData(DataTablesPageRequest pageRequest, string productId)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistDocumentByProductRequest request = new RetrieveChecklistDocumentByProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.Checklist"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    ProductId = productId
                };
                var customerRecords = new RetrieveChecklistDocumentByProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistDocumentByProduct(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistDocumentRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.DocumentGroupCategory,
                                                             x.DocumentGroup,
                                                             x.Document,
                                                             x.Original,
                                                             x.Copy,
                                                             x.Duplicate,
                                                             x.Note,
                                                             x.PictureDoc,
                                                             x.Id
                        }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult CustChecklistDocumentData(DataTablesPageRequest pageRequest, string productId)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistDocumentByProductRequest request = new RetrieveChecklistDocumentByProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.Checklist"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    ProductId = productId
                };
                var customerRecords = new RetrieveChecklistDocumentByProductResponse();
                customerRecords = serviceClient.Proxy.RetrieveChecklistDocumentByProduct(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistDocumentRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.DocumentGroupCategory,
                                                             x.DocumentGroup,
                                                             x.isCheck,
                                                             x.Document,
                                                             x.Original,
                                                             x.Copy,
                                                             x.Duplicate,
                                                             x.Note,
                                                             x.PictureDoc,
                                                             x.Id
                        }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult ViewChecklistCustomerData(DataTablesPageRequest pageRequest, string ccid)
        {

            DataTableResponse pageResponse = null;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                int index = pageRequest.SearchColumns.FindIndex(x => !string.IsNullOrEmpty(x));
                RetrieveViewChecklistCustomerByCcidRequest request = new RetrieveViewChecklistCustomerByCcidRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.Checklist"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    Ccid = ccid,
                    Filter = new FilterRecord(),
                };
                var customerRecords = new RetrieveViewChecklistCustomerByCcidResponse();
                customerRecords = serviceClient.Proxy.RetrieveViewChecklistCustomerByCcid(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistDocumentRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.DocumentGroupCategory,
                                                             x.DocumentGroup,
                                                             x.isCheck,
                                                             x.Document,
                                                             x.Original,
                                                             x.Copy,
                                                             x.Duplicate,
                                                             x.Note,
                                                             x.PictureDoc,
                                                             x.Id
                        }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult ViewChecklistCustomerDatatable(string ccid)
        {
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveViewChecklistCustomerByCcidRequest request = new RetrieveViewChecklistCustomerByCcidRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.Checklist"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    Ccid = ccid,
                    Filter = new FilterRecord(),
                };
                var customerRecords = new RetrieveViewChecklistCustomerByCcidResponse();
                customerRecords = serviceClient.Proxy.RetrieveViewChecklistCustomerByCcid(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistDocumentRecord>() : customerRecords.Records;
                var aaData = resultCustomer.Select(x => new object[]{
                                                             x.DocumentGroupCategory,
                                                             x.DocumentGroup,
                                                             x.isCheck,
                                                             x.Document,
                                                             x.Original,
                                                             x.Copy,
                                                             x.Duplicate,
                                                             x.Note,
                                                             x.PictureDoc,
                                                             x.Id
                        }).ToList();
                return Json(aaData);
            };
        }
        [Authorize]
        [HttpPost]
        public ActionResult SubmitQuestion(string data, string productId)
        {
            var customerRecords = new RetrieveChecklistDocumentByProductResponse();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveChecklistDocumentByProductRequest request = new RetrieveChecklistDocumentByProductRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.Checklist"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    QuestionData = data,
                    ProductId = productId
                };
                customerRecords = serviceClient.Proxy.RetrieveChecklistDocumentByProduct(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ChecklistDocumentRecord>() : customerRecords.Records;
                var aaData = resultCustomer.Select(x => new object[]{
                                                             x.DocumentGroupCategory,
                                                             x.DocumentGroup,
                                                             x.isCheck,
                                                             x.Document,
                                                             x.Original,
                                                             x.Copy,
                                                             x.Duplicate,
                                                             x.Note,
                                                             x.PictureDoc,
                                                             x.Id
                        }).ToList();
                aaData.Add(new object[]{
                    resultCustomer.FirstOrDefault().SubProductId,
                    customerRecords.SubProductName
                });
                return Json(aaData);
            }
        }

        [Authorize]
        [HttpPost]
        public ActionResult SubmitData(string dataedit, string productName)
        {
            bool isUpdate = false;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                ResponseRecord response = null;
                UpdateCustomerChecklistRequest request = new UpdateCustomerChecklistRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.EditAccountPlanningFinanceAction"
                    },
                    DataUpdate = dataedit,
                    DaoChange = CurrentUserRecord,
                    SubProductId = int.Parse(productName)
                };
                response = serviceClient.Proxy.UpdateCustomerChecklist(request);
                isUpdate = response.IsSuccess;
            }
            return Json(isUpdate);
        }

        [Authorize]
        [HttpPost]
        public ActionResult UpdateChecklist(string dataedit, string ccid)
        {
            bool isUpdate = false;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                ResponseRecord response = null;
                UpdateCustomerChecklistRequest request = new UpdateCustomerChecklistRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.PortfolioManagementController.EditAccountPlanningFinanceAction"
                    },
                    DataUpdate = dataedit,
                    DaoChange = CurrentUserRecord,
                    Ccid = ccid
                };
                response = serviceClient.Proxy.UpdateCustomerChecklist(request);
                isUpdate = response.IsSuccess;
            }
            return Json(isUpdate);
        }

        #endregion

        #region Sale contest


        [CustomActionFilter]
        [Authorize]
        public ActionResult SaleContest()
        {
            ProductHandbookViewModel vm = new ProductHandbookViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveSaleContestProgramRequest request = new RetrieveSaleContestProgramRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.ByProduct"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                };
                var customerRecords = new RetrieveSaleContestProgramResponse();
                customerRecords = serviceClient.Proxy.RetrieveSaleContestProgram(request);
                vm.productCat = (customerRecords == null || customerRecords.Records == null) ? new List<ProductHandbookCategoryRecord>() : customerRecords.Records;
            }
            return View(vm);
        }



        [CustomActionFilter]
        [Authorize]
        public ActionResult SaleContestInfo(string salecontest)
        {
            SaleContestInfoViewModel vm = new SaleContestInfoViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveSaleContestInfoBySaleContestIdRequest request = new RetrieveSaleContestInfoBySaleContestIdRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.ProductDetail"
                    },
                    Lang = CurrentLang,
                    SaleContestId = string.IsNullOrEmpty(salecontest) ? 0 : Convert.ToInt32(salecontest),
                    CurrentUser = CurrentUserRecord
                };
                var customerRecords = new RetrieveSaleContestInfoBySaleContestIdResponse();
                customerRecords = serviceClient.Proxy.RetrieveSaleContestInfoBySaleContestId(request);
                vm.SaleContestInfo = (customerRecords == null || customerRecords.Record == null) ? new SaleContestProgramRecord() : customerRecords.Record;
            }
            return View(vm);
        }

        #endregion
    }
}
